<?
defined('_JEXEC') or die('Restricted access');

class TableCoupon extends JTable
{
    public $id=0;
    public $uniqueCode='';
    public $productName='';
    public $catid=0;
    public $userId=0;
    public $productPrice=0.0;
    public $discountPercent=0.0;
    public $currency='руб';
    public $startSellingDate=null;
    public $endSellingDate=null;
    public $totalCount=0;
    public $restCount=0;
    public $about='';
    public $state=0;
    public $target='';
    
    public function __construct($db) {
        parent::__construct('#__coupons', 'id', $db);
    }
    
    public function check() 
    {   
        return $this->decimalCheck()&& $this->intCheck() && $this->datetimeCheck();
    }
    
    private function datetimeCheck()
    {
        if ($this->startSellingDate==null || $this->endSellingDate==null)
            return false;
            
        if(!LogicHelper::isDateTime($this->startSellingDate) || !LogicHelper::isDateTime($this->endSellingDate))
            return false;
        
        $startDate = LogicHelper::getDateFromString($this->startSellingDate);
        $endDate = LogicHelper::getDateFromString($this->endSellingDate); 
        
        return $startDate < $endDate;
    }
    
    private function decimalCheck()
    {
        return LogicHelper::isDouble($this->productPrice) && LogicHelper::isDouble($this->discountPercent) &&
        (double)$this->discountPercent >= 0.0 && (double)$this->discountPercent < 100.0 && (double)$this->productPrice >= 0.0;
    }
    
    private function intCheck()
    {
        $isNewRecord = true;
        if($this->id!=null)
            $isNewRecord = LogicHelper::isInt($this->id);
        return $isNewRecord && LogicHelper::isInt($this->totalCount) && LogicHelper::isInt($this->restCount) && 
               LogicHelper::isInt($this->catid) && LogicHelper::isInt($this->userId) &&
               (int)$this->totalCount >= 0 && (int)$this->restCount >= 0 && (int)$this->restCount <= (int)$this->totalCount;
    }
    
    public function publish($pks = null, $state = 1, $userId = 0) {
        // Sanitize input.
        JArrayHelper::toInteger($pks);
        $userId = (int) $userId;
        $state  = (int) $state;

        // If there are no primary keys set check to see if the instance key is set.
        if (empty($pks))
            $pks = array($this->_tbl_key);

        // Get an instance of the table
        $table = JTable::getInstance('Coupon', 'Table');

        foreach ($pks as $pk)
        {
                if(!$table->load($pk))
                    $this->setError($table->getError());

                $table->state = $state;

                $table->check();

                if (!$table->store())
                        $this->setError($table->getError());
        }
        return count($this->getErrors())==0;
    }


    public function store($updateNulls = false) {
        if($this->userId < 1)
        {
            $user = JFactory::getUser();
            $this->userId = $user->get('id');
        }
        
        if($this->id < 1)
        {
            if( $this->totalCount > 0)
            {
                for($i=0; $i< $this->totalCount; $i++)
                        $strArray[] = LogicHelper::generateCouponCodes(15);
                $this->uniqueCode = implode(' ', $strArray) ;
            }
            $this->restCount = $this->totalCount;
        }
        if(!$this->check())
            return false;
        
        return parent::store($updateNulls);
    }
}
?>
